<?php


namespace app\common\model;


use think\facade\Db;

class SalesmanFinanceDaily
{
    /**
     * 业务员业绩统计
     * @param array $salesmanInfoArr
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function runStat($salesmanInfoArr) {
        // 1、订单金额是结算价  订单总数：统所有产品的充值成功订单数量
        foreach ($salesmanInfoArr as $salesmanId => $info) {
            foreach ($info as $servType => $item) {
                foreach ($item as $date => $v) {
                    $statId = Db::name('salesman_finance_daily')
                        ->where([
                            'salesman_id' => $salesmanId,
                            'dateymd' => $date,
                            'serv_type' => $servType,
                        ])
                        ->value('id');
                    if ($statId) {
                        // 日期存在 追加当前数据
                        Db::name('salesman_finance_daily')
                            ->where('id', $statId)
                            ->inc('order_total', $v['order_total'])
                            ->inc('amount', $v['amount'])
                            ->update();
                    } else {
                        Db::name('salesman_finance_daily')
                            ->insert([
                                'salesman_id' => $salesmanId,
                                'dateymd' => $date,
                                'order_total' => $v['order_total'],
                                'amount' => $v['amount'],
                                'serv_type' => $servType,
                            ]);
                    }
                }
            }
        }
    }
}